#include <iostream>
using namespace std;
#include "stock1.h"
#include <string.h>

Stock::Stock() {
  cout << "调用了无参的构造函数" << endl;
  strcpy(company, "no name");
  shares = 0;
  share_val = 0.0;
  total_val = 0.0;
}

Stock::Stock(const char *co, int n, double pr) {
  cout << "调用了有参数的构造函数" << endl;
  strncpy(company, co, 29);
  company[29] = '\0';

  if (n < 0) {
    perror("err\n");
    exit(1);
  } else {
    shares = n;
    share_val = pr;
    set_tot();
  }
}

Stock::~Stock() { cout << "bye," << company << endl; }

//其他函数的实现、
void Stock::buy(int num, double price) {
  if (num < 0) {
    perror("err\n");
  } else {
    shares += num;
    share_val = price;
    set_tot();
  }
}

void Stock::sell(int num, double price) {
  if (num < 0) {
    perror("err\n");
  } else if (num > shares) {
    perror("err\n");
  } else {
    shares -= num;
    share_val = price;
    set_tot();
  }
}

void Stock::update(double price) {
  share_val = price;
  set_tot();
}

void Stock::show() {
  cout << "company: " << company << " shares:" << shares
       << " share_val:" << share_val << " total_val:" << total_val << endl;
}
